
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.Socket;
import javax.imageio.ImageIO;

public class RemoteClient extends JFrame {
    private JLabel screenLabel;
    private Socket socket;
    private Robot robot;
    
    public RemoteClient(String serverIP) {
        try {
            socket = new Socket(serverIP, 12345);
            robot = new Robot();
            
            setTitle("远程桌面控制客户端");
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setSize(800, 600);
            
            screenLabel = new JLabel();
            screenLabel.setHorizontalAlignment(JLabel.CENTER);
            screenLabel.setVerticalAlignment(JLabel.CENTER);
            
            JScrollPane scrollPane = new JScrollPane(screenLabel);
            add(scrollPane);
            
            // 启动屏幕接收线程
            new Thread(this::receiveScreen).start();
            
            // 添加鼠标监听
            screenLabel.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    sendMouseEvent(2, e.getButton(), e.getX(), e.getY());
                }
            });
            
            screenLabel.addMouseMotionListener(new MouseMotionAdapter() {
                @Override
                public void mouseMoved(MouseEvent e) {
                    sendMouseEvent(1, 0, e.getX(), e.getY());
                }
            });
            
            // 添加键盘监听
            addKeyListener(new KeyAdapter() {
                @Override
                public void keyPressed(KeyEvent e) {
                    sendKeyEvent(3, e.getKeyCode());
                }
            });
            
            setVisible(true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    private void receiveScreen() {
        try {
            InputStream in = socket.getInputStream();
            
            while (!socket.isClosed()) {
                DataInputStream dis = new DataInputStream(in);
                int length = dis.readInt();
                byte[] imageBytes = new byte[length];
                dis.readFully(imageBytes);
                
                BufferedImage image = ImageIO.read(new ByteArrayInputStream(imageBytes));
                ImageIcon icon = new ImageIcon(image);
                screenLabel.setIcon(icon);
            }
        } catch (Exception e) {
            System.out.println("屏幕接收线程终止");
        }
    }
    
    private void sendMouseEvent(int type, int button, int x, int y) {
        System.out.println("mouse event:" + type + "," + button + "," + x + "," + y);
        try {
            OutputStream out = socket.getOutputStream();
            DataOutputStream dos = new DataOutputStream(out);
            dos.writeInt(type);
            dos.writeInt(button);
            dos.writeInt(x);
            dos.writeInt(y);
            dos.flush();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    private void sendKeyEvent(int type, int keyCode) {
        try {
            OutputStream out = socket.getOutputStream();
            DataOutputStream dos = new DataOutputStream(out);
            dos.writeInt(type);
            dos.writeInt(keyCode);
            dos.flush();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    public static void main(String[] args) {
        String serverIP = JOptionPane.showInputDialog("请输入服务器IP地址:");
        if (serverIP != null && !serverIP.isEmpty()) {
            new RemoteClient(serverIP);
        }
    }
}
